package com.example.ljw.frescowallpaper.view;

import android.app.WallpaperManager;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.ljw.frescowallpaper.R;
import com.example.ljw.frescowallpaper.helper.HttpURLConnHelper;
import com.example.ljw.frescowallpaper.helper.SDCardHelper;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.tencent.qq.QQ;

public class DownloadDetailsActivity extends AppCompatActivity implements View.OnClickListener{

    private ImageView imageView_detail;
    private Button wallpaper,delete,star,share,back;
    private Bitmap bitmap;
    private WallpaperManager wallpaperManager;
    private HashMap<String,Object> hashMap = new HashMap<String, Object>();


    //从我的下载页面传过来的点击位置
    private int position;
    private File file;
    private String url;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //隐藏标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //隐藏状态栏
        //定义全屏参数
        int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
        //获得当前窗体对象
        Window window = DownloadDetailsActivity.this.getWindow();
        //设置当前窗体为全屏显示
        window.setFlags(flag, flag);
        setContentView(R.layout.activity_download_details);
        ShareSDK.initSDK(this);
        hashMap.put("ShareByAppClient","true");

        initData();
        initView();

    }

    private void initData() {
        Intent intent = getIntent();
        position = intent.getIntExtra("position",1);

//        Toast.makeText(this, position, Toast.LENGTH_SHORT).show();
        File path = new File(SDCardHelper.getSDCardPublicDir(Environment.DIRECTORY_DOWNLOADS));
        File[] files = path.listFiles();
        file = files[position];

    }

    private void initView() {
        imageView_detail= (ImageView) findViewById(R.id.imageView_detail);
        wallpaper= (Button) findViewById(R.id.wallpaper);
        wallpaper.setOnClickListener(this);
        delete= (Button) findViewById(R.id.delete);
        delete.setBackgroundResource(R.mipmap.delete);
        delete.setOnClickListener(this);
        star= (Button) findViewById(R.id.star);
        star.setOnClickListener(this);
        share= (Button) findViewById(R.id.share);
        share.setOnClickListener(this);
        back= (Button) findViewById(R.id.btn_back);
        back.setOnClickListener(this);


        Glide.with(this)
                .load(file)
                .into(imageView_detail);

        imageView_detail.setDrawingCacheEnabled(true);


    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.wallpaper:
                Toast.makeText(DownloadDetailsActivity.this,"壁纸更换成功",Toast.LENGTH_LONG).show();
                try {
                    wallpaperManager = WallpaperManager.getInstance(this);
                    wallpaperManager.setBitmap(imageView_detail.getDrawingCache());


                } catch (IOException e) {
                    e.printStackTrace();

                }


                break;
            case R.id.delete:
                File path = new File(SDCardHelper.getSDCardPublicDir(Environment.DIRECTORY_DOWNLOADS));

                File[] files = path.listFiles();
                file = files[position];
                String name = file.getName();
                SDCardHelper.removeFileFromSDCard(path+"/"+name);
                Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
                finish();
                break;
            case R.id.star:
                break;
            case R.id.share:
                showShare();
                break;
            case R.id.btn_back:
                finish();
                break;
        }

    }
    private void showShare() {
        ShareSDK.initSDK(this,"1915371b11e92");
        hashMap.put("Id","7");
        hashMap.put("SortId","7");
        hashMap.put("AppId","1105825500");
        hashMap.put("AppKey","e6tCxEP7UV30RaYz");
        hashMap.put("Enable","true");
        ShareSDK.setPlatformDevInfo(QQ.NAME,hashMap);
        OnekeyShare oks = new OnekeyShare();

        //关闭sso授权
        oks.disableSSOWhenAuthorize();


        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间等使用
        oks.setTitle("一起来换壁纸吧!");
        // titleUrl是标题的网络链接，QQ和QQ空间等使用
        oks.setTitleUrl("http://mob.com");
        // text是分享文本，所有平台都需要这个字段
        oks.setText("最新的壁纸都在这");
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        oks.setImageUrl(url);

        // oks.setImagePath("/sdcard/zby1.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
//        oks.setUrl("http://sharesdk.cn");
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
//        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
//        oks.setSite(getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
//        oks.setSiteUrl("http://sharesdk.cn");

        // 启动分享GUI
        oks.show(this);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        imageView_detail.setDrawingCacheEnabled(false);


    }

    /**
     * 创建指定数量的随机字符串
     * @param numberFlag 是否是数字
     * @param length
     * @return
     */
    public static String createRandom(boolean numberFlag, int length){
        String retStr = "";
        String strTable = numberFlag ? "1234567890" : "1234567890abcdefghijkmnpqrstuvwxyz";
        int len = strTable.length();
        boolean bDone = true;
        do {
            retStr = "";
            int count = 0;
            for (int i = 0; i < length; i++) {
                double dblR = Math.random() * len;
                int intR = (int) Math.floor(dblR);
                char c = strTable.charAt(intR);
                if (('0' <= c) && (c <= '9')) {
                    count++;
                }
                retStr += strTable.charAt(intR);
            }
            if (count >= 2) {
                bDone = false;
            }
        } while (bDone);

        return retStr;
    }

}
